const moment = require('moment')
const mongoose = require('../config/dbHelper')
const Schema = mongoose.Schema
const LinksSchema = new Schema({
  title: { type: String, default: '' }, // 标题
  link: { type: String, default: '' }, // 链接
  type: { type: String, default: 'link' }, // tips 温馨提示，links友情链接
  created: { type: Date, default: '' }, // 创建时间
  updated: { type: Date, default: '' }, // 更新时间
  isTop: { type: String, default: '' }, // 是否置顶
  sort: { type: String, default: '' } // 排序编号
})

LinksSchema.pre('save', function(next) {
  this.created = moment().format('YYYY-MM-DD HH:mm:ss')
  next()
})

LinksSchema.pre('update', function(next) {
  this.updated = moment().format('YYYY-MM-DD HH:mm:ss')
  next()
})

LinksSchema.statics = {
  /**
   * 列表
   * @param {*} type tips：温馨提醒 links：友情链接
   */
  getList (type) {
    return this.find({ type })
  }
}

const LinksModel = mongoose.model('links', LinksSchema)
module.exports = LinksModel
